package com.twu.biblioteca;

public class BibliotecaApp {

    Library library = new Library();

    public static void main(String[] args) {
        BibliotecaApp app = new BibliotecaApp();

        String output = app.run(args);

        System.out.println(output);
    }

    // this is clearly not optimal
    // this class is now responsible for:
    //   - dispatching commands
    //   - creating output
    //   - user auth
    public String run(String[] argv) {

        Args args = Args.parse(argv);
        String command = args.getCommand();
        User user = null;

        if (args.getLibraryNumber() != null) {
            user = User.authenticate(args.getLibraryNumber(), args.getPassword());
            if (user == null) {
                return invalidLoginText();
            }
        }

        if (command == null) {
            return welcomeText();
        } else if (command.equals("help")) {
            return helpText();
        } else if (command.equals("list")) {
            return listText();
        } else if (command.equals("find")) {
            return findText(args.getItemTitle());
        } else if (command.equals("userinfo")) {
            return userInfoText(user);
        } else if (command.equals("checkout")) {
            return runCheckOut(args.getItemID(), user);
        } else if (command.equals("checkin")) {
            return runCheckIn(args.getItemID());
        } else {
            return unrecognizedCommandText(command);
        }


    }

    private String runCheckOut(int targetItemID, User user) {
        String output;
        Item checkedOutItem = library.checkOut(targetItemID, user);

        if (checkedOutItem != null) {
            output = successfulCheckOutText(checkedOutItem);
        } else {
            output = unsuccessfulCheckOutText();
        }
        return output;
    }

    private String runCheckIn(int targetItemID) {
        String output;

        Item itemToReturn = library.checkIn(targetItemID);

        if (itemToReturn != null) {
            output = successfulCheckInText(itemToReturn);
        } else {
            output = unsuccessfulCheckInText();
        }

        return output;
    }

    private String welcomeText() {
        String welcome = "Welcome to the Record Management System of the Bangalore Public Library\n";
        return (welcome + listText());
    }

    private String helpText() {
        String helpText = "";

        helpText += "Here's a list of supported commands\n";
        helpText += "help                            # shows this text\n";
        helpText += "list                            # lists all the items in the Library record\n";
        helpText += "find <itemTitle>                # returns items matching the query (Useful for finding IDs)";
        helpText += "<libraryNumber> <password> userinfo                        # displays user info\n";
        helpText += "<libraryNumber> <password> checkout <itemID>               # checks out the item to costumer\n";
        helpText += "<libraryNumber> <password> checkin <itemID>                # checks in the item\n";
        helpText += "Sample usage:\n";
        helpText += "$ biblioteca list\n";

        return helpText;
    }

    private String listText() {
        return library.record.list();
    }

    private String findText(String itemTitle) {
        return library.record.find(itemTitle);
    }

    private String invalidLoginText() {
        return "Unrecognized library number or password.";
    }

    private String userInfoText(User user) {
        return user.info();
    }

    private String successfulCheckOutText(Item checkedOutBook) {
        String checkoutText = "";
        checkoutText +=  "You have successfully checked out:\n";
        checkoutText += checkedOutBook.toString() + "\n";
        checkoutText += "Thank you, enjoy the item!\n";
        return checkoutText;
    }

    private String unsuccessfulCheckOutText() {
        return "That item is not available! (Item not found or already checked out by someone else)\n";
    }

    private String successfulCheckInText(Item checkedInItem) {
        return "Thank you for returning: " + checkedInItem.toString();
    }

    private String unsuccessfulCheckInText() {
        return "That is not a valid item to return";
    }

    private String unrecognizedCommandText(String command) {
       return "The command \"" + command + "\" is unrecognized!\n" + helpText();
    }

}
